<?php

namespace app\shopapi\controller;

use think\Db;
/**
 * 发票
 * Class Goods
 * @package app\api\controller
 */
class Invoice extends Controller
{
    private $user;
    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        // 用户信息
        $this->user = $this->getUser();
    }
    public function apply()
    {
        $data = $this->postData();
        // 简单校验
        if (empty($data['order_id'])  || empty($data['invoice_title']) || empty($data['invoice_type'])) {
            return $this->renderError('缺少必要参数');
        }

        if ($data['invoice_type'] == "邮寄" && empty($data['consignee_address'])) {
            return $this->renderError('邮寄地址不能为空');
        }

        if ($data['invoice_type'] == "邮箱" && empty($data['email'])) {
            return $this->renderError('邮箱地址不能为空');
        }
        // 校验订单是否已经开过票
        $invoice = Db::name('invoice')
            ->where('order_id', $data['order_id'])
            ->where('user_id', $this->user['user_id'])
            ->find();
        if ($invoice) {
            return $this->renderError('该订单已开过票，请勿重复申请');
        }
        // 获取订单金额
        $amount = Db::name('order')->where('order_id', $data['order_id'])->value('pay_price');
        // 插入数据
        $invoiceId = Db::name('invoice')->insertGetId([
            'user_id'       => $this->user['user_id'], // 若有 token 登录
            'order_id'      => $data['order_id'],
            'title_type'      => $data['title_type'],
            'amount'        => $amount,
            'invoice_title'         => $data['invoice_title'],
            'tax_number'    => $data['tax_number'] ?? '',
            'invoice_type'     => $data['invoice_type'],
            'invoice_content'     => $data['invoice_content'],
            'consignee_name'  => $data['consignee_name'],
            'consignee_mobile' => $data['consignee_mobile'],
            'email'         => $data['email'] ?? '',
            'consignee_address'       => $data['consignee_address'] ?? '',
            'status'        => 0,
            'create_time'   => date('Y-m-d H:i:s'),
            'update_time'   => date('Y-m-d H:i:s'),
        ]);

        return $this->renderSuccess('开票申请提交成功', ['invoice_id' => $invoiceId]);
    }
    // 获取用户最新开票记录的地址以及邮箱
    public function getLatestInvoiceInfo()
    {
        $invoice = Db::name('invoice')
            ->where('user_id', $this->user['user_id'])
            ->where('order_id', input()['order_id'])
            ->order('create_time', 'desc')
            ->find();

        return $this->renderSuccess('获取成功', $invoice);
    }
    // 查看开票进度
    public function getInvoiceProgress()
    {
        $data = $this->postData();
        $invoice = Db::name('invoice')
            ->where('order_id', $data['order_id'])
            ->where('user_id', $this->user['user_id']) 
            ->find();

        if (!$invoice) {
            return $this->renderError('开票记录不存在');
        }
        // 处理开票状态
        $invoice['status_text'] = $this->getInvoiceStatus($invoice['status']);
        return $this->renderSuccess('获取成功', $invoice);
    }
    // 0=待处理, 1=已开票, 2=已邮寄/已发送, 3=驳回
    public function getInvoiceStatus($status)
    {
        switch ($status) {
            case 0:
                return '待处理';
            case 1:
                return '已开票';
            case 2:
                return '已邮寄/已发送';
            case 3:
                return '驳回';
            default:
                return $this->renderError('未知状态');
        }
    }
}